<?php
LZ_MODULE != 'admin' && die('Access Denied');
$action = $_GET['action'];
$m = $_GET['m'];
$_authorized = false;
$category_id = intval($_GET['category_id']);


$item_id = intval($_GET['item_id']);

include_once('model/category.php');
$category = new LZ_Category;
include_once('model/item.php');
$item = new LZ_Item;


//添加文章
if ($m == 'new_item')
{
	$data = filter_array($_POST,'name!,description!,intval:category_id!,intval:order_id,ck_url_name:url_name!');
	$_p = get_item_path(array(
			'url_name'=>$data['url_name'],
			'publish_time'=>time()
		));
	if (!$_p)
	{
		$action = 'new_item';
		$err_msg = lang('ITEM_FILL_ALL');
	}
	else if (file_exists(LZ_TOPPATH.$_p))
	{
		$action = 'new_item';
		$err_msg = lang('ITEM_EXISTS');
	}
	else if ($data)
	{
		$data['publish_time'] = time();
		$data['user_id'] = $_SESSION['login_user']['user_id'];
		$data['url'] = $_p;
		if ( $item->add( $data ) )
		{
			lz_make_item($_p,array('id'=>$db->insert_id()));
			lz_exit(lang('NEW_ITEM_SUCCESS'),'admin.php?p=item',1);
		}
		else
		{
			$action = 'new_item';
			$err_msg = lang('ITEM_UNKNOWN_ERROR');
		}
	}
	else
	{
		$action = 'new_item';
		$err_msg = lang('ITEM_FILL_ALL');;
	}
}
else if ($m == 'edit_item')
{
	$data = filter_array($_POST,'name!,description!,intval:category_id!,intval:order_id');
	$item_arr = $item->get_one($item_id);

	if ($data)
	{
		$data['update_time'] = time();
		$data['update_user_id'] = $_SESSION['login_user']['user_id'];
		if ( $item->update( $item_id, $data) )
		{
			lz_exit(lang('ITEM_EDIT_SUCCESS'),'admin.php?p=item',1);
		}
		else
		{
			$action = 'edit_item';
			$err_msg = lang('ITEM_UNKNOWN_ERROR');
		}
	}
	else
	{
		$action = 'edit_item';
		$err_msg = lang('ITEM_FILL_ALL');
	}
}
else if ($m == 'delete_item')
{
	if ($item_id)
	{
		$item_arr = $item->get_one($item_id);
		if ($item->delete($item_id))
		{
			lz_exit( lang('ITEM_DELETE_SUCCESS') ,"admin.php?p=item",1);
		}
		else
		{
			lz_exit( lang('ITEM_DELETE_ERROR'), "admin.php?p=item",3);
		}
	}
}


//添加项目
if ($action == 'new_item')
{
	include_once('plugin/fckeditor/fckeditor.php');
	$temp = template('item_new.html');
	$temp->assign( array(
		'item' => $_POST,
		'categories' => $category->get_list(),
		'category_id' => $category_id,
		'login_user' => $_SESSION['login_user'],
	));
	$view_data['page_description'] = lang('NEW_ITEM');
	$view_data['page_content'] = $temp->result();
}
//编辑项目
else if ($action == 'edit_item')
{
	include_once('plugin/fckeditor/fckeditor.php');
	$temp = template('item_new.html');
	$temp->assign( array(
		'item' => (count($_POST)>0)?$_POST:$item->get_one($item_id),
		'item_id' => $item_id,
		'categories' => $category->get_list(),
		'category_id' => $category_id,
		'login_user' => $_SESSION['login_user'],
	));
	$view_data['page_description'] = lang('EDIT_ITEM');
	$view_data['page_content'] = $temp->result();
}
//显示分类和项目列表
else
{
	$categories_arr = $category->get_list();
	$categories = array();
	foreach($categories_arr as $c)
	{
		$categories[$c['category_id']] = $c['name'];
	}

	//分页处理
	$items = lz_page($item,array(),(intval($config['admin_item_page_size']))?intval($config['admin_item_page_size']):50);
	
	for($i=0;$i<count($items);$i++)
	{
		if ($items[$i]['publish_time'])
		{
			$items[$i]['publish_time'] = date('Y-m-d H:i',$items[$i]['publish_time']);
		}
		if ($items[$i]['update_time'])
		{
			$items[$i]['update_time'] = date('Y-m-d H:i',$items[$i]['update_time']);
		}
		$_c = $categories[$items[$i]['category_id']];
		$items[$i]['category_name'] = $_c?$_c:lang('DEFAULT_CATEGORY');
	}
	
	$view_data['categories'] = $categories_arr;
	$view_data['items'] = $items;
	$view_data['page_description'] = lang('ITEM_LIST');
}

$view_data['to_category'] = $to_category;
$view_data['real_content'] = "category_new.html";
$view_data['err_msg'] = $err_msg;
$view_data['success_msg'] = $success_msg;
$view_data['category_id'] = $category_id;
?>